/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package inet.dao;

import inet.entity.Department;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author MiSoKo
 */
public class DepartmentDAO extends BaseDAO {

    //Hiển thị tất cả
    public List<Department> getAll() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = null;
        List<Department> lstDep = null;
        try {
            sql = "SELECT * FROM DEPARTMENTS";
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            Department dep = null;
            while (rs.next()) {
                dep = new Department();
                if (lstDep == null) {
                    lstDep = new ArrayList<Department>();
                }
                int i = 1;
                dep.setDep_id(rs.getBigDecimal(i++));
                dep.setDep_name(rs.getString(i++));
                dep.setDescription(rs.getString(i++));
                dep.setLocaltion(rs.getString(i++));
                dep.setBranch_id(rs.getBigDecimal(i++));
                lstDep.add(dep);
            }
            return lstDep;
        } catch (Exception ex) {
            System.out.println("Error in sql: " + sql);
            ex.printStackTrace();
        } finally {
            releaseConnection(conn, ps, rs);
        }
        return null;
    }

    //Hien thi theo ID
    public Department getById(BigDecimal id) {
        Department dep = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT * FROM DEPARTMENTS "
                + "WHERE DEP_ID = ?";

        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            ps.setBigDecimal(1, id);
            rs = ps.executeQuery();
            int i = 1;
            while (rs.next()) {
                dep = new Department();
                dep.setDep_id(rs.getBigDecimal(i++));
                dep.setDep_name(rs.getString(i++));
                dep.setDescription(rs.getString(i++));
                dep.setLocaltion(rs.getString(i++));
                dep.setBranch_id(rs.getBigDecimal(i++));
//                System.out.print(rs.getString(2));
//                System.out.print(rs.getString(3));
//                System.out.print(rs.getString(4));
            }
            return dep;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            releaseConnection(conn, ps, rs);
        }
        return null;
    }

    //Hiển thị theo mã Branch ID
    public List<Department> getDepinBranch(BigDecimal branchid) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = null;
        List<Department> lstDep = null;
        try {
            sql = "SELECT * FROM DEPARTMENTS WHERE BRANCH_ID = ?";
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            ps.setBigDecimal(1, branchid);
            rs = ps.executeQuery();
            Department dep = null;
            while (rs.next()) {
                dep = new Department();
                if (lstDep == null) {
                    lstDep = new ArrayList<Department>();
                }
                int i = 1;
                dep.setDep_id(rs.getBigDecimal(i++));
                dep.setDep_name(rs.getString(i++));
                dep.setDescription(rs.getString(i++));
                dep.setLocaltion(rs.getString(i++));
                dep.setBranch_id(rs.getBigDecimal(i++));
                lstDep.add(dep);
            }
            return lstDep;
        } catch (Exception ex) {
            System.out.println("Error in sql: " + sql);
            ex.printStackTrace();
        } finally {
            releaseConnection(conn, ps, rs);
        }
        return null;
    }

//    public static void main(String[] a) {
//        DepartmentDAO d = new DepartmentDAO();
//        if (d.getDepinBranch(BigDecimal.ONE) == null) {
//            System.out.print("loi");
//        } else {
//            System.out.print("ok");
//        }
//    }
}
